package com.mall.server.controller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.commons.utils.JwtUtils;
import com.mall.server.service.AdminService;
import com.tanhua.model.domain.UmsAdmin;
import com.tanhua.model.domain.UmsRole;
import com.tanhua.model.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 登录
     *
     * @param umsAdmin
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody UmsAdmin umsAdmin) {
        if (umsAdmin.getPassword() == null || umsAdmin.getUsername() == null) {
            return R.error(ErrorResult.emptyError().getErrMessage());
        }

        UmsAdmin login = adminService.login(umsAdmin.getUsername(), umsAdmin.getPassword());
        if (login.getStatus() == 0) {
            return R.error("账号已被禁用!");
        }
        if (login == null) {
            throw new BusinessException(ErrorResult.longError());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", login.getId());
        map.put("username", login.getUsername());
        String token = JwtUtils.getToken(map);

        HashMap<String, String> map2 = new HashMap<>();
        map2.put("token", token);

        return R.success(map2);
    }


    /**
     * 返回值
     * {
     * "code":200,
     * "message":"操作成功",
     * "data":{
     * "roles":Array[1],
     * "icon":"http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg.jpg",
     * "menus":Array[23],
     * "username":"admin"
     * }
     * }
     *
     * @return
     */
    @GetMapping("/info")
    public R info() {
        Map map = adminService.info();
        return R.success(map);
    }


    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public R logout() {
        adminService.logout();
        return R.success("");
    }

    /**
     * 根据用户名或姓名分页获取用户列表
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R list(String keyword, Integer pageNum, Integer pageSize) {
        PageVo vo = adminService.list(keyword, pageNum, pageSize);
        if (vo == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return R.success(vo);
    }


    /**
     * 用户注册
     *
     * @param umsAdmin
     * @return
     */

    @PostMapping("/register")
    public R register(@RequestBody UmsAdmin umsAdmin) {

        boolean register = adminService.register(umsAdmin);
        if (register) {
            return R.success(200);
        }
        return R.success(ErrorResult.emptyError());
    }


    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        adminService.delete(id);
        return R.success("操作成功");
    }

    /**
     * 修改帐号状态
     *
     * @param id
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable Integer id, Integer status) {
        adminService.updateStatus(id, status);
        return R.success("操作成功");
    }

    /**
     * 修改用户信息
     *
     * @param umsAdmin
     * @return
     */
    @PostMapping("/update/{id}")
    public R update(@RequestBody UmsAdmin umsAdmin) {
        adminService.update(umsAdmin);
        return R.success("操作成功");
    }

    /**
     * 获取指定用户的角色
     *
     * @param adminId
     * @return
     */
    @GetMapping("/role/{adminId}")
    public R adminId(@PathVariable Integer adminId) {
        List<UmsRole> umsRole = adminService.adminId(adminId);
        return R.success(umsRole);
    }


    /**
     * 给用户分配角色
     *
     * @param adminId
     * @return
     */
    @PostMapping("/role/update")
    public R roleUpdate(Integer adminId, Integer[] roleIds) {
        adminService.roleUpdate(adminId, roleIds);
        return R.success("操作成功");
    }


}
